package com.dongzhi.web.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.web.filter.GenericFilterBean;

import com.opensymphony.module.sitemesh.Config;
import com.opensymphony.module.sitemesh.Factory;
import com.opensymphony.module.sitemesh.factory.DefaultFactory;

public class SitemeshAppEngineHelperFilter extends GenericFilterBean {
	/** Web context lookup key */
	private static final String SITEMESH_FACTORY = "sitemesh.factory";

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		if (getServletContext().getAttribute(SITEMESH_FACTORY) == null) {
			Factory instance = new DefaultFactory(new Config(getFilterConfig()));
			getServletContext().setAttribute(SITEMESH_FACTORY, instance);
		}
		filterChain.doFilter(request, response);
	}
}
